using Newtonsoft.Json.Linq;
using XchUserInfo.DAL;
using XchUserInfo.Model;
using System.Threading.Tasks;
using XchUserInfo.Common;

namespace XchUserInfo.BLL
{
    [Service(EServLife.Scoped)]
    public class XchUserBO : IXchUserBO
    {
        private readonly XchUserDAO _dao;
        public XchUserBO(XchUserDAO dao)
        {
            _dao = dao;
        }

        public Task<XchUser?> GetAsync(int id) => _dao.GetAsync(id);

        public Task<int> AddAsync(XchUser entity) => _dao.InsertAsync(entity);

        public async Task<bool> UpdateAsync(int id, XchUser entity)
        {
            entity.XchUserID = id;
            return await _dao.UpdateAsync(entity);
        }

        public Task<bool> DeleteAsync(int id) => _dao.DeleteAsync(id);

        public async Task<PagedList<XchUser>> PageAsync(PagedListReq req)
        {
            string? nameLike = null, emailLike = null, phoneLike = null;
            if (req.condition is JObject jo)
            {
                nameLike = (string?)jo["XchUserName"];
                emailLike = (string?)jo["XchUserEmail"];
                phoneLike = (string?)jo["XchUserPhone"];
            }
            return await _dao.PageAsync(req.pageIndex, req.pageSize, nameLike, emailLike, phoneLike);
        }
    }
}


